// RuleJudge.Judge.NTM
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
// using System.Windows.Media.Media3D;
// using Common.Enum;
using System.Windows.Forms;
using RuleJudge.Judge;
using RuleJudge.RuleData;
using SLANCommon.Enum;

public class NTM : BaseJudge
{
    public NTM()
    {
        Type = EProjectType.AQ;
        base.TubesNo = new List<int> { 1, 1, 1, 1, 1, 2, 2, 2, 2, 2 };
        base.ChannelsNo = new List<int> { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 };
    }

    int FAM = 0;
    int VIC = 1;
    int ROX = 2;
    int CY5 = 3;
    int Quasar705 = 4;

    bool NC_Check_Pass = false;
    bool PC_Check_Pass = false;
    bool Leak_NC_Control = false;
    bool Leak_PC_Control = false;

    const double IC025_Cutoff = 35.0;
    const double PC025_Cutoff = 35.0;
    const double M_xen_Cutoff = 35.0;
    const double M_che_Cutoff = 35.0;
    const double M_avi_Cutoff = 35.0;
    const double M_int_Cutoff = 35.0;
    const double M_for_Cutoff = 35.0;
    const double M_kan_Cutoff = 35.0;
    const double M_abs_Cutoff = 35.0;
    const double M_gor_Cutoff = 35.0;

    // 反应管A/B各通道检测靶标
    //      0         1        2       3
    //  |-----------------------------------|
    //  |   FAM  |   VIC  |   ROX  |   CY5  |
    //  |  M.xen |  M.che |  M.avi |  M.int |
    //  |-----------------------------------|
    //  |   FAM  |   FAM  |   VIC  |   ROX  |
    //  |  M.for |  M.kan |  M.abs |  M.gor |
    //  |-----------------------------------|

    // 😹🐱‍🐉🐵🐶🐺🐱🦁🐯🦒🦊🦝🐮🐷🐗🐭🐹🐰🐻🐨🐼🐸🦓🐴🦄🐔🐲🐒🐕‍🦺🐫🦘🦙🐏🐁🐘🦨🦡🦛🦏🐪🐇🦔🐀🐿🦎🐊🐢🐍🐋🐬🐉🐟🐠🐡🦐🦑🐙🦞🦀🦜🦆🐓🦚🦉🐦🐤🦗🐜🐝🐌🦋从🕷🐞🦂🦇🦃🦅
    // 开始因阳性质控、待测(未知)样本判读
    public override string Judge(List<Sample> samples)
    {
        if (samples.Count == 0)
        {
            return "无判断样本！";
        }

        #region 阴性指控判断
        List<Sample> NC_list = samples.Where((Sample s) => s.Type == SampleType.Negative).ToList();
        if (NC_list.Count != 0)
        {
            foreach (Sample NC in NC_list)
            {
                NC_Check_Pass = NC.Targets.Where((Target s) => double.IsInfinity(s.Result.Ct) && s.ChannelNo != 5).Count() == NC.Targets.Where((Target s) => s.ChannelNo != 5).Count() &&
                                NC.Targets.Where((Target s) => s.Result.Ct <= IC025_Cutoff && s.ChannelNo == 5).Count() == NC.Targets.Where((Target s) => s.ChannelNo == 5).Count();
                NC.Tubes[0].Targets[ROX].Conclusion.Add(new Conclusion
                {
                    Content = (NC_Check_Pass ? "阴性对照合格" : "阴性对照不合格"),
                    Color = (NC_Check_Pass ? Colors.Green : Colors.Red),
                    BackgroundColor = (NC_Check_Pass ? Colors.LightGreen : Colors.LightYellow),
                    ForegroundColor = (NC_Check_Pass ? Colors.Red : Colors.Blue)
                });
                NC.Tubes[1].Targets[ROX].Conclusion.Add(new Conclusion
                {
                    Content = (NC_Check_Pass ? "阴性对照合格" : "阴性对照不合格"),
                    Color = (NC_Check_Pass ? Colors.Green : Colors.Red),
                    BackgroundColor = (NC_Check_Pass ? Colors.LightGreen : Colors.LightYellow),
                    ForegroundColor = (NC_Check_Pass ? Colors.Red : Colors.Blue)
                });
            }
        }
        else
        {
            Leak_NC_Control = true;
        }
        #endregion

        // 😹🐱‍🐉🐵🐶🐺🐱🦁🐯🦒🦊🦝🐮🐷🐗🐭🐹🐰🐻🐨🐼🐸🦓🐴🦄🐔🐲🐒🐕‍🦺🐫🦘🦙🐏🐁🐘🦨🦡🦛🦏🐪🐇🦔🐀🐿🦎🐊🐢🐍🐋🐬🐉🐟🐠🐡🦐🦑🐙🦞🦀🦜🦆🐓🦚🦉🐦🐤🦗🐜🐝🐌🦋从🕷🐞🦂🦇🦃🦅
        #region 阳性质控判读
        List<Sample> PC_list = samples.Where((Sample s) => s.Type == SampleType.HighPositive).ToList();
        if (PC_list.Count != 0)
        {
            foreach (Sample PC in PC_list)
            {
                PC_Check_Pass = PC.Targets.Where((Target s) => s.Result.Ct <= PC025_Cutoff).Count() == PC.Targets.Count;
                PC.Tubes[0].Targets[ROX].Conclusion.Add(new Conclusion
                {
                    Content = (PC_Check_Pass ? "阳性对照合格" : "阳性对照不合格"),
                    Color = (PC_Check_Pass ? Colors.Green : Colors.Red),
                    BackgroundColor = (PC_Check_Pass ? Colors.LightGreen : Colors.LightYellow)
                });
                PC.Tubes[1].Targets[ROX].Conclusion.Add(new Conclusion
                {
                    Content = (PC_Check_Pass ? "阳性对照合格" : "阳性对照不合格"),
                    Color = (PC_Check_Pass ? Colors.Green : Colors.Red),
                    BackgroundColor = (PC_Check_Pass ? Colors.LightGreen : Colors.LightYellow)
                });
            }
        }
        else
        {
            Leak_PC_Control = true;
        }
        #endregion

        // 😹🐱‍🐉🐵🐶🐺🐱🦁🐯🦒🦊🦝🐮🐷🐗🐭🐹🐰🐻🐨🐼🐸🦓🐴🦄🐔🐲🐒🐕‍🦺🐫🦘🦙🐏🐁🐘🦨🦡🦛🦏🐪🐇🦔🐀🐿🦎🐊🐢🐍🐋🐬🐉🐟🐠🐡🦐🦑🐙🦞🦀🦜🦆🐓🦚🦉🐦🐤🦗🐜🐝🐌🦋从🕷🐞🦂🦇🦃🦅
        #region 待测样本判读
        List<Sample> UnknownSample_list = samples.Where((Sample s) => s.Type == SampleType.Unknown).ToList();
        foreach (Sample Unknown_Sample in UnknownSample_list)
        {
            // 样本内参是否合格:（1）反应孔A、B均有非内参靶标阳性；（2）反应孔A、B把表均为阴性，且内参阳性。
            bool IC_Check_Pass = true;
            if (Unknown_Sample.Tubes[0].Targets.Where((Target s) => s.Result.Ct > IC025_Cutoff && s.ChannelNo != 5).Count() == Unknown_Sample.Tubes[0].Targets.Where((Target s) => s.ChannelNo != 5).Count()  // 非内参通道的Ct均大于35
                && Unknown_Sample.Tubes[0].Targets[Quasar705].Result.Ct > IC025_Cutoff)
            {
                IC_Check_Pass = false;
            }
            if (Unknown_Sample.Tubes[1].Targets.Where((Target s) => s.Result.Ct > IC025_Cutoff && s.ChannelNo != 5).Count() == Unknown_Sample.Tubes[1].Targets.Where((Target s) => s.ChannelNo != 5).Count()  // 非内参通道的Ct均大于35
                && Unknown_Sample.Tubes[1].Targets[Quasar705].Result.Ct > IC025_Cutoff)
            {
                IC_Check_Pass = false;
            }
            if (!IC_Check_Pass)
            {
                Unknown_Sample.Conclusion.Add(new Conclusion
                {
                    Content = "内参检测不合格，检测结果无效"
                });
                continue;
            }
            // 反应孔A的FAM通道：M.xen 蟾蜍分枝杆菌
            Unknown_Sample.Tubes[0].Targets[FAM].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[0].Targets[FAM].Result.Ct <= M_xen_Cutoff ? "蟾蜍分枝杆菌：阳性" : "蟾蜍分枝杆菌：阴性",
                Color = Unknown_Sample.Tubes[0].Targets[FAM].Result.Ct <= M_xen_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[0].Targets[FAM].Result.Ct <= M_xen_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应孔A的VIC通道：M.che 龟分枝杆菌
            Unknown_Sample.Tubes[0].Targets[VIC].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[0].Targets[VIC].Result.Ct <= M_che_Cutoff ? "龟分枝杆菌：阳性" : "龟分枝杆菌：阴性",
                Color = Unknown_Sample.Tubes[0].Targets[VIC].Result.Ct <= M_che_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[0].Targets[VIC].Result.Ct <= M_che_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应孔A的ROX通道：M.avi 鸟分枝杆菌
            Unknown_Sample.Tubes[0].Targets[ROX].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[0].Targets[ROX].Result.Ct <= M_avi_Cutoff ? "鸟分枝杆菌：阳性" : "鸟分枝杆菌：阴性",
                Color = Unknown_Sample.Tubes[0].Targets[ROX].Result.Ct <= M_avi_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[0].Targets[ROX].Result.Ct <= M_avi_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应孔A的CY5通道：M.int 胞内分枝杆菌
            Unknown_Sample.Tubes[0].Targets[CY5].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[0].Targets[CY5].Result.Ct <= M_int_Cutoff ? "胞内分枝杆菌：阳性" : "胞内分枝杆菌：阴性",
                Color = Unknown_Sample.Tubes[0].Targets[CY5].Result.Ct <= M_int_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[0].Targets[CY5].Result.Ct <= M_int_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应孔B的FAM通道：M.for 偶发分枝杆菌
            Unknown_Sample.Tubes[1].Targets[FAM].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[1].Targets[FAM].Result.Ct <= M_for_Cutoff ? "偶发分枝杆菌：阳性" : "偶发分枝杆菌：阴性",
                Color = Unknown_Sample.Tubes[1].Targets[FAM].Result.Ct <= M_for_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[1].Targets[FAM].Result.Ct <= M_for_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应孔B的VIC通道：M.kan 堪萨斯分枝杆菌
            Unknown_Sample.Tubes[1].Targets[VIC].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[1].Targets[VIC].Result.Ct <= M_kan_Cutoff ? "偶发分枝杆菌：阳性" : "偶发分枝杆菌：阴性",
                Color = Unknown_Sample.Tubes[1].Targets[VIC].Result.Ct <= M_kan_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[1].Targets[VIC].Result.Ct <= M_kan_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应孔B的ROX通道：M.abs 脓肿分枝杆菌
            Unknown_Sample.Tubes[1].Targets[ROX].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[1].Targets[ROX].Result.Ct <= M_abs_Cutoff ? "脓肿分枝杆菌：阳性" : "脓肿分枝杆菌：阴性",
                Color = Unknown_Sample.Tubes[1].Targets[ROX].Result.Ct <= M_abs_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[1].Targets[ROX].Result.Ct <= M_abs_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应孔B的CY5通道：M.gor 戈登分枝杆菌
            Unknown_Sample.Tubes[1].Targets[CY5].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[1].Targets[CY5].Result.Ct <= M_gor_Cutoff ? "戈登分枝杆菌：阳性" : "戈登分枝杆菌：阴性",
                Color = Unknown_Sample.Tubes[1].Targets[CY5].Result.Ct <= M_gor_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[1].Targets[CY5].Result.Ct <= M_gor_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
        }
        #endregion

        // 😹🐱‍🐉🐵🐶🐺🐱🦁🐯🦒🦊🦝🐮🐷🐗🐭🐹🐰🐻🐨🐼🐸🦓🐴🦄🐔🐲🐒🐕‍🦺🐫🦘🦙🐏🐁🐘🦨🦡🦛🦏🐪🐇🦔🐀🐿🦎🐊🐢🐍🐋🐬🐉🐟🐠🐡🦐🦑🐙🦞🦀🦜🦆🐓🦚🦉🐦🐤🦗🐜🐝🐌🦋从🕷🐞🦂🦇🦃🦅
        #region 重测样本判读
        List<Sample> RetestSample_list = samples.Where((Sample s) => s.Type == SampleType.RetestSample).ToList();
        foreach (Sample Retest_Sample in RetestSample_list)
        {
            // 样本内参是否合格:（1）反应孔A、B均有非内参靶标阳性；（2）反应孔A、B把表均为阴性，且内参阳性。
            bool IC_Check_Pass = true;
            if (Retest_Sample.Tubes[0].Targets.Where((Target s) => s.Result.Ct > IC025_Cutoff && s.ChannelNo != 5).Count() == Retest_Sample.Tubes[0].Targets.Where((Target s) => s.ChannelNo != 5).Count()  // 非内参通道的Ct均大于35
                && Retest_Sample.Tubes[0].Targets[Quasar705].Result.Ct > IC025_Cutoff)
            {
                IC_Check_Pass = false;
            }
            if (Retest_Sample.Tubes[1].Targets.Where((Target s) => s.Result.Ct > IC025_Cutoff && s.ChannelNo != 5).Count() == Retest_Sample.Tubes[1].Targets.Where((Target s) => s.ChannelNo != 5).Count()  // 非内参通道的Ct均大于35
                && Retest_Sample.Tubes[1].Targets[Quasar705].Result.Ct > IC025_Cutoff)
            {
                IC_Check_Pass = false;
            }
            if (!IC_Check_Pass)
            {
                Retest_Sample.Conclusion.Add(new Conclusion
                {
                    Content = "内参检测不合格，检测结果无效"
                });
                continue;
            }
            // 反应孔A的FAM通道：M.xen 蟾蜍分枝杆菌
            Retest_Sample.Tubes[0].Targets[FAM].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[0].Targets[FAM].Result.Ct <= M_xen_Cutoff ? "蟾蜍分枝杆菌：阳性" : "蟾蜍分枝杆菌：阴性",
                Color = Retest_Sample.Tubes[0].Targets[FAM].Result.Ct <= M_xen_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[0].Targets[FAM].Result.Ct <= M_xen_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应孔A的VIC通道：M.che 龟分枝杆菌
            Retest_Sample.Tubes[0].Targets[VIC].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[0].Targets[VIC].Result.Ct <= M_che_Cutoff ? "龟分枝杆菌：阳性" : "龟分枝杆菌：阴性",
                Color = Retest_Sample.Tubes[0].Targets[VIC].Result.Ct <= M_che_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[0].Targets[VIC].Result.Ct <= M_che_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应孔A的ROX通道：M.avi 鸟分枝杆菌
            Retest_Sample.Tubes[0].Targets[ROX].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[0].Targets[ROX].Result.Ct <= M_avi_Cutoff ? "鸟分枝杆菌：阳性" : "鸟分枝杆菌：阴性",
                Color = Retest_Sample.Tubes[0].Targets[ROX].Result.Ct <= M_avi_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[0].Targets[ROX].Result.Ct <= M_avi_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应孔A的CY5通道：M.int 胞内分枝杆菌
            Retest_Sample.Tubes[0].Targets[CY5].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[0].Targets[CY5].Result.Ct <= M_int_Cutoff ? "胞内分枝杆菌：阳性" : "胞内分枝杆菌：阴性",
                Color = Retest_Sample.Tubes[0].Targets[CY5].Result.Ct <= M_int_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[0].Targets[CY5].Result.Ct <= M_int_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应孔B的FAM通道：M.for 偶发分枝杆菌
            Retest_Sample.Tubes[1].Targets[FAM].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[1].Targets[FAM].Result.Ct <= M_for_Cutoff ? "偶发分枝杆菌：阳性" : "偶发分枝杆菌：阴性",
                Color = Retest_Sample.Tubes[1].Targets[FAM].Result.Ct <= M_for_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[1].Targets[FAM].Result.Ct <= M_for_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应孔B的VIC通道：M.kan 堪萨斯分枝杆菌
            Retest_Sample.Tubes[1].Targets[VIC].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[1].Targets[VIC].Result.Ct <= M_kan_Cutoff ? "偶发分枝杆菌：阳性" : "偶发分枝杆菌：阴性",
                Color = Retest_Sample.Tubes[1].Targets[VIC].Result.Ct <= M_kan_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[1].Targets[VIC].Result.Ct <= M_kan_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应孔B的ROX通道：M.abs 脓肿分枝杆菌
            Retest_Sample.Tubes[1].Targets[ROX].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[1].Targets[ROX].Result.Ct <= M_abs_Cutoff ? "脓肿分枝杆菌：阳性" : "脓肿分枝杆菌：阴性",
                Color = Retest_Sample.Tubes[1].Targets[ROX].Result.Ct <= M_abs_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[1].Targets[ROX].Result.Ct <= M_abs_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应孔B的CY5通道：M.gor 戈登分枝杆菌
            Retest_Sample.Tubes[1].Targets[CY5].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[1].Targets[CY5].Result.Ct <= M_gor_Cutoff ? "戈登分枝杆菌：阳性" : "戈登分枝杆菌：阴性",
                Color = Retest_Sample.Tubes[1].Targets[CY5].Result.Ct <= M_gor_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[1].Targets[CY5].Result.Ct <= M_gor_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
        }
        #endregion

        /* *****************************************************************************************************
         * 实验中未设置阴阳性质控不影响判读，仅给出弹窗提醒缺少质控 *************************************************
         * 阴阳性质控不合格的，则给出提醒，但仍然进行判读 **********************************************************
         * *****************************************************************************************************
         */
        #region 质控结果异常（不合格，缺失）提示
        if (!NC_Check_Pass || !PC_Check_Pass)
        {
            System.Windows.Forms.MessageBox.Show("一个或多个阴阳性质控不符合要求！是否忽略？", "质控提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        if (Leak_NC_Control || Leak_PC_Control)
        {
            System.Windows.Forms.MessageBox.Show("实验缺少阴性质控或阳性质控！是否忽略？", "质控提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        #endregion

        return string.Empty;
    }
}